package rstplus;

/*
 * RST - Russian SMS transliterator
 * Copyleft 2011
 * Author: Alexey Shat'ko (Алексей Шатько), rstapp@yandex.ru
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this source code (in file copying.txt); if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


import java.util.Calendar;
import java.util.Date;


// !!! This class is not intended to represend date not within the XXI century !!!
public class SerializableDate {
    
    private Date date;
    
    public static final int SERIALIZED_SIZE = 6;
    
    public SerializableDate(Date date) {
        this.date = date;
    }
    
    public SerializableDate(byte[] record) {
        if(record.length!=6) {
            throw new IllegalArgumentException();
        }
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, (int)record[0]+2000);
        cal.set(Calendar.MONTH, record[1]);
        cal.set(Calendar.DAY_OF_MONTH, record[2]);
        cal.set(Calendar.HOUR_OF_DAY, record[3]);
        cal.set(Calendar.MINUTE, record[4]);
        cal.set(Calendar.SECOND, record[5]);
        date = cal.getTime();
    }
    
    public byte[] toByteArray() {
        byte[] record = new byte[6];
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        record[0] = (byte)(cal.get(Calendar.YEAR)-2000);
        record[1] = (byte)cal.get(Calendar.MONTH);
        record[2] = (byte)cal.get(Calendar.DAY_OF_MONTH);
        record[3] = (byte)cal.get(Calendar.HOUR_OF_DAY);
        record[4] = (byte)cal.get(Calendar.MINUTE);
        record[5] = (byte)cal.get(Calendar.SECOND);
        return record;
    }
    
    public Date getDate() {
        return date;
    }
    
    public int compareTo(SerializableDate date) {
        long t1 = this.date.getTime();
        long t2 = date.date.getTime();
        if(t1<t2) {
            return -1;
        } else if(t1>t2) {
            return 1;
        } else {
            return 0;
        }
    }
    
    public String toString() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        String year = String.valueOf(cal.get(Calendar.YEAR));
        String month = monthToString(cal.get(Calendar.MONTH));
        String day = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
        if(day.length()==1) {
            day = "0"+day;
        }
        String hour = String.valueOf(cal.get(Calendar.HOUR_OF_DAY));
        if(hour.length()==1) {
            hour = "0"+hour;
        }
        String minute = String.valueOf(cal.get(Calendar.MINUTE));
        if(minute.length()==1) {
            minute = "0"+minute;
        }
        String second = String.valueOf(cal.get(Calendar.SECOND));
        if(second.length()==1) {
            second = "0"+second;
        }
        return day+"."+month+"."+year+" "+hour+":"+minute+":"+second;
    }
    
    private static String monthToString(int month) {
          switch(month) {
            case Calendar.JANUARY:
                return "01";
            case Calendar.FEBRUARY:
                return "02";
            case Calendar.MARCH:
                return "03";
            case Calendar.APRIL:
                return "04";
            case Calendar.MAY:
                return "05";
            case Calendar.JUNE:
                return "06";
            case Calendar.JULY:
                return "07";
            case Calendar.AUGUST:
                return "08";
            case Calendar.SEPTEMBER:
                return "09";
            case Calendar.OCTOBER:
                return "10";
            case Calendar.NOVEMBER:
                return "11";
            case Calendar.DECEMBER:
                return "12";
            default:
                throw new IllegalArgumentException();
        }      
    }
}
